<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    //创建函数，生成当前时间字符串
    //格式： 2021-10-19 10:10:20

    function f(num){
        return num <10?"0"+num:num;
    }

    function time(){
        var date2 = new Date();
        var hour = date2.getHours();
        var minute = date2.getMinutes();
        var second = date2.getSeconds();
        // if(hour < 10){
        //     hour = "0" + hour;
        // }
        // if(minute < 10){
        //     minute = "0" + minute;
        // }
        // if(second < 10){
        //     second = "0" + second;
        // }
        // console.log(date2.getFullYear() +"-"+(date2.getMonth()+1) +"-"+date2.getDate()+" "+hour+":"+minute+":"+second);
        return(date2.getFullYear() +"-"+(date2.getMonth()+1) +"-"+date2.getDate()+" "+f(hour)+":"+f(minute)+":"+f(second));
    }
    document.write(time());
</script>
</html>